/* Copyright Statement
 all these pde files(Processing source code) are written by me (Huaming Li) and my younger daughter Chuyi Li.
 all the other material and data, including but not limited to:
 coding, architecture design, class design, flow control,  bird image file, color scheme, sound
 are composed by Chuyi Li
 
 the cute bird(parrot) is one of our 2 pet birds - Mango. We will show another bird in future project.
 the bird image is drawn using a touch-pen in Microsoft Concepts on HP ENVY touch-screen laptop, with some edit workload in GIMP.
 
 Mircrosoft Concetps
 https://apps.microsoft.com/detail/9ngqm8fph9wq?hl=en-us&gl=US
 
 to download GIMP
 https://www.gimp.org/downloads/
 
 to download Processing
 https://processing.org/download
 
 */
import processing.sound.*;
PFont font;
String copyRight = "Hanoi show step by step. Chuyi Li. Sep 16 2024";
String version = " Ver2.3";

PImage imgStop, imgBird;

void loadSound() {
  loadSoundFile();
  assignBtnSound(); // must be called after loadSoundFile()
}

void setup() {
  thread("loadSound");

  size(1800, 900);
  smooth();

  rectMode(RADIUS);
  frameRate(100);

  strokeWeight(2);

  font = loadFont("VinerHandITC-128.vlw");
  textFont(font);

  // ====== {
  imgStop = loadImage("stop.png");
  btnStop.img = imgStop; // stop button is an image button
  imgBird = loadImage("pet_bird.png");
  // ======= }

  preparePlates(numPlates); // initial power up plates

  println(copyRight + version); // last action in setup()
}

void draw() {
  paintBackground(); // 1st action in draw()
  draw3Poles(); // 2nd action in draw()

  // === {
  draw10plates();
  showProgress();
  showMovingPlateInfo();
  drawProgressBar();  // progress bar on the bottom
  // ==== }

  hanioShow(); // 2nd last action in draw()
  drawMouseClickCircle(); // last action in draw()
}

void mouseClicked() {
  checkMouseClick(mouseX, mouseY);
  // println(mouseX + " " + mouseY); // degug usage
}

void paintBackground() {
  background(#D4E9FA);

  // copyright message
  fill(#0BD3C4); // text color
  textSize(50);
  text(copyRight, 10, 60);

  drawNumPlates(); // information of number of plates only
  drawallBtns();
}

// ======== mouse click on button circle animation  ======== //
/*
I don't think this kind of simple behavior needs a class
 or any timer to handle.
 just use draw() paint it by frameRate is perfect
 */
int mcc_radius = 0;
int mcc_x, mcc_y;
void drawMouseClickCircle() {
  if (mcc_radius > 0) {
    noFill();
    stroke(#F50707);
    circle(mcc_x, mcc_y, 50 - mcc_radius);
    mcc_radius -= 3;
  }
}

void setMouseClickCirlce(int x, int y) {
  mcc_x = x;
  mcc_y = y;
  mcc_radius = 20;
}

// ======== sound files for button click  ======== //
SoundFile sndUp  = null;
SoundFile sndDn = null;
SoundFile sndGo = null;
SoundFile sndPause = null;
SoundFile sndFast = null;
SoundFile sndMedium = null;
SoundFile sndSlow = null;
SoundFile sndDone = null;
SoundFile sndDrop = null;

/*
SoundFile sndUp = new SoundFile(this, "up.mp3");
 is not allowed
 */
void loadSoundFile() {
  sndUp = new SoundFile(this, "up.mp3");
  sndDn = new SoundFile(this, "down.mp3");
  sndGo = new SoundFile(this, "go.mp3");
  sndPause = new SoundFile(this, "pause.mp3");
  sndFast = new SoundFile(this, "fast.mp3");
  sndMedium = new SoundFile(this, "medium.mp3");
  sndSlow = new SoundFile(this, "slow.mp3");
  sndDone = new SoundFile(this, "done.mp3");
  sndDrop = new SoundFile(this, "drop.mp3");
}

void playSound(SoundFile snd) {
  if (snd != null) {
    snd.play(); // if sound file is not loaded yet, don't play it.
  }
}
